﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using KProject.Model;

namespace KProject.DAL
{
    static class JuradosDAL
    {
        public static List<Jurado> List(int idConcurso)
        {
            SQLiteCommand cmd = new SQLiteCommand("select * from Jurado where idConcurso = @idConcurso");
            cmd.Parameters.Add("@idConcurso", DbType.Int32).Value = idConcurso;

            return CommonDAL.List(cmd, dr => new Jurado()
                    {
                        Id = int.Parse(dr["idJurado"].ToString()),
                        Nome = dr["nomeJurado"].ToString()
                    });
        }

        public static int Remove(int? id)
        {
            SQLiteCommand cmd = new SQLiteCommand("delete from Jurado where idJurado = @idJurado");
            cmd.Parameters.Add("@idJurado", DbType.Int32).Value = id;

            return CommonDAL.ExecuteNonQuery(cmd);
        }

        public static string GetNameOf(int? id)
        {
            SQLiteCommand cmd = new SQLiteCommand("select nomeJurado from Jurado where idJurado = @idJurado");
            cmd.Parameters.Add("@idJurado", DbType.Int32).Value = id;

            return CommonDAL.ExecuteScalar(cmd).ToString();
        }

        public static int Insert(string nome, int idConcurso)
        {
            List<SQLiteCommand> cmds = new List<SQLiteCommand>();

            SQLiteCommand cmd = new SQLiteCommand("insert into Jurado (nomeJurado, idConcurso) values (@nomeJurado, @idConcurso);");
            cmd.Parameters.Add("@nomeJurado", DbType.String, 100).Value = nome;
            cmd.Parameters.Add("@idConcurso", DbType.Int32).Value = idConcurso;
            cmds.Add(cmd);

            cmd = new SQLiteCommand("select last_insert_rowid() from Jurado;");
            cmds.Add(cmd);

            return Convert.ToInt32(CommonDAL.ExecuteTransaction(cmds, 1));
        }

        public static int Update(int? id, string nome)
        {
            SQLiteCommand cmd = new SQLiteCommand("update Jurado set nomeJurado = @nomeJurado where idJurado = @idJurado");
            cmd.Parameters.Add("@nomeJurado", DbType.String, 100).Value = nome;
            cmd.Parameters.Add("@idJurado", DbType.Int16).Value = id;

            return CommonDAL.ExecuteNonQuery(cmd);
        }
    }
}
